﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectronicRecords.Models;
using System.Web.Security;

namespace ElectronicRecords.Controllers
{

    public class ProcedursController : Controller
    {
        //
        // GET: /Procedurs/
        private DataManager _dataManager;
        public ProcedursController(DataManager dataManager)
        {
            _dataManager = dataManager;
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List()
        {
            ViewData["Title"] = "Справочник процедур";
            ViewData["Procedurs"] = _dataManager.Procedurs.GetAllProcedurs();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            Procedurs p=new Procedurs();
            List<SelectListItem> equipments = new List<SelectListItem>();
            List<Equipments> l = _dataManager.Equipments.GetEquipments().ToList();
            equipments.Add(new SelectListItem() { Value = (0).ToString(), Text = "Без оборудования", Selected=true });
            for (int i = 0; i < l.Count(); i++)
            {
                equipments.Add(new SelectListItem() { Value = l[i].ID_Equipment.ToString(), Text = l[i].Name });
            }
            ViewProcedure vp = new ViewProcedure();
            vp.p = p;
            vp.equipments = equipments;


            return View(vp);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Procedurs p, int equipments)
        {
            if (p.Description == null || p.Description.Length < 1) ModelState.AddModelError("p.Description", "Неверное значение в поле");
            if (p.Name == null || p.Name.Length < 1) ModelState.AddModelError("p.Name", "Неверное значение в поле");
            if ((p.Length % 30) != 0) ModelState.AddModelError("p.Length", "Длительность процедуры должна быть кратна 30 минутам");

            if (ModelState.IsValid)
            {
                //c.ID_Client = id;
                MembershipUser u = Membership.GetUser(User.Identity.Name);
                if (equipments != 0)
                    p.ID_Equipment = equipments;
                else p.ID_Equipment = null;
                p.CreatedBy = (Guid)u.ProviderUserKey;
                _dataManager.Procedurs.CreateProcedure(p);
                return RedirectToAction("List");
            }

             p = new Procedurs();
            List<SelectListItem> equipment = new List<SelectListItem>();
            List<Equipments> l = _dataManager.Equipments.GetEquipments().ToList();
            equipment.Add(new SelectListItem() { Value = (0).ToString(), Text = "Без оборудования", Selected = true });
            for (int i = 0; i < l.Count(); i++)
            {
                equipment.Add(new SelectListItem() { Value = l[i].ID_Equipment.ToString(), Text = l[i].Name });
            }
            ViewProcedure vp = new ViewProcedure();
            vp.p = p;
            vp.equipments = equipment;


            return View(vp);

        }

        public ActionResult Procedure(int id)
        {
            ViewData.Model = _dataManager.Procedurs.GetProcedure(id);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
           // ViewData.Model = _dataManager.Procedurs.GetProcedure(id);
            Procedurs p = _dataManager.Procedurs.GetProcedure(id);
            List<SelectListItem> equipments =new List<SelectListItem>();
            List<Equipments> l =_dataManager.Equipments.GetEquipments().ToList();
            if (p.ID_Equipment == null) equipments.Add(new SelectListItem() { Value=(0).ToString(), Text="Без оборудования",Selected=true});
            else equipments.Add(new SelectListItem() { Value = (0).ToString(), Text = "Без оборудования"});
            for (int i = 0; i < l.Count(); i++)
            {

                if (p.ID_Equipment == null || l[i].ID_Equipment != p.ID_Equipment)
                {
                    equipments.Add(new SelectListItem() { Value = l[i].ID_Equipment.ToString(), Text = l[i].Name });
                }
                else
                {
                    equipments.Add(new SelectListItem() { Value = l[i].ID_Equipment.ToString(), Text = l[i].Name, Selected = true });
                } 
            }
            ViewProcedure vp = new ViewProcedure();
            vp.p = p;
            vp.equipments = equipments;
           
                
            return View(vp);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Procedurs p, int equipments)
        {
            if (p.Description == null || p.Description.Length < 1) ModelState.AddModelError("p.Description", "Неверное значение в поле");
            if (p.Name == null || p.Name.Length < 1) ModelState.AddModelError("p.Name", "Неверное значение в поле");
            if ((p.Length % 30) != 0) ModelState.AddModelError("p.Length", "Длительность процедуры должна быть кратна 30 минутам");
            

            if (ModelState.IsValid)
            {
                p.ID_Procedure = id;
                MembershipUser u = Membership.GetUser(User.Identity.Name);

                p.CreatedBy = (Guid)u.ProviderUserKey;
                if (equipments != 0)
                    p.ID_Equipment = equipments;
                else p.ID_Equipment = null;
                _dataManager.Procedurs.UpdateProcedure(p);
                return RedirectToAction("List");
            }

            //заново заполняем дропдаун
            p = _dataManager.Procedurs.GetProcedure(id);
            List<SelectListItem> equipment = new List<SelectListItem>();
            List<Equipments> l = _dataManager.Equipments.GetEquipments().ToList();
            if (p.ID_Equipment == null) equipment.Add(new SelectListItem() { Value = (0).ToString(), Text = "Без оборудования", Selected = true });
            else equipment.Add(new SelectListItem() { Value = (0).ToString(), Text = "Без оборудования" });
            for (int i = 0; i < l.Count(); i++)
            {

                if (p.ID_Equipment == null || l[i].ID_Equipment != p.ID_Equipment)
                {
                    equipment.Add(new SelectListItem() { Value = l[i].ID_Equipment.ToString(), Text = l[i].Name });
                }
                else
                {
                    equipment.Add(new SelectListItem() { Value = l[i].ID_Equipment.ToString(), Text = l[i].Name, Selected = true });
                }
            }
            ViewProcedure vp = new ViewProcedure();
            vp.p = p;
            vp.equipments = equipment;
            
            

            return View(vp);


        }
    }
}
